/**
 * <pre>
   Copyright 2016 aaron.sd

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * </pre>
 *
 * @author aaronwong.sd@foxmail.com
 * @version 下午10:06:24
 */
package com.mc.adapter.wechat.mpAPI;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration {

	private static Properties defaultProperty;

	static {
		init();
	}

	static void init() {
		// 初始化默认配置
		defaultProperty = new Properties();
		// 读取自定义配置
		String t4jProps = "config/weixin.properties";
		boolean loaded = loadProperties(defaultProperty, "." + File.separatorChar + t4jProps)
				|| loadProperties(defaultProperty, Configuration.class.getResourceAsStream("/WEB-INF/" + t4jProps))
				|| loadProperties(defaultProperty, Configuration.class.getClassLoader().getResourceAsStream(t4jProps));
		if (!loaded) {
			System.out.println("iwechat:没有加载到weixin.properties属性文件!");
		}
	}

	/**
	 * 加载属性文件
	 *
	 * @param props
	 *            属性文件实例
	 * @param path
	 *            属性文件路径
	 * @return 是否加载成功
	 */
	private static boolean loadProperties(Properties props, String path) {
		try {
			File file = new File(path);
			if (file.exists() && file.isFile()) {
				props.load(new FileInputStream(file));
				return true;
			}
		} catch (IOException ignore) {
			// 异常忽略
			ignore.printStackTrace();
		}
		return false;
	}

	/**
	 * 加载属性文件
	 *
	 * @param props
	 *            属性文件实例
	 * @param is
	 *            属性文件流
	 * @return 是否加载成功
	 */
	private static boolean loadProperties(Properties props, InputStream is) {
		try {
			if (is != null) {
				props.load(is);
				return true;
			}
		} catch (IOException ignore) {
			// 异常忽略
			ignore.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取开发者第三方用户唯一凭证
	 *
	 * @return 第三方用户唯一凭证
	 */
	public static String getOAuthAppId() {
		return getProperty("iwechat.oauth.appid");
	}

	/**
	 * 获取开发者第三方用户唯一凭证
	 *
	 * @param appid
	 *            默认第三方用户唯一凭证
	 * @return 第三方用户唯一凭证
	 */
	public static String getOAuthAppId(String appid) {
		return getProperty("iwechat.oauth.appid", appid);
	}

	/**
	 * 获取开发者第三方用户唯一凭证密钥
	 *
	 * @return 第三方用户唯一凭证密钥
	 */
	public static String getOAuthSecret() {
		return getProperty("iwechat.oauth.secret");
	}

	/**
	 * 获取开发者第三方用户唯一凭证密钥
	 *
	 * @param secret
	 *            默认第三方用户唯一凭证密钥
	 * @return 第三方用户唯一凭证密钥
	 */
	public static String getOAuthSecret(String secret) {
		return getProperty("iwechat.oauth.secret", secret);
	}

	/**
	 * 获取 是否为调试模式
	 *
	 * @return 是否为调试模式
	 */
	public static boolean isDebug() {
		return getBoolean("iwechat.debug");
	}

	public static boolean getBoolean(String name) {
		String value = getProperty(name);
		return Boolean.valueOf(value);
	}

	public static int getIntProperty(String name) {
		String value = getProperty(name);
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException nfe) {
			return -1;
		}
	}

	public static int getIntProperty(String name, int fallbackValue) {
		String value = getProperty(name, String.valueOf(fallbackValue));
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException nfe) {
			return -1;
		}
	}

	/**
	 * 获取属性值
	 *
	 * @param name
	 *            属性名称
	 * @return 属性值
	 */
	public static String getProperty(String name) {
		return getProperty(name, null);
	}

	/**
	 * 获取属性值
	 *
	 * @param name
	 *            属性名
	 * @param fallbackValue
	 *            默认返回值
	 * @return 属性值
	 */
	public static String getProperty(String name, String fallbackValue) {
		String value = defaultProperty.getProperty(name, fallbackValue);
		return value;
	}
}
